package com.jerry.template.module.welcome.adapter;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.support.v4.view.PagerAdapter;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import com.jerry.template.module.home.activity.HomeActivity;

/**
 * Created by chenhuan on 2016/5/10.
 */
public class WelcomeAdapter extends PagerAdapter{
    private Context context;
    private int[] id;

    public WelcomeAdapter(Context context, int[] id){
        this.context = context;
        this.id = id;
    }

    @Override
    public int getCount(){
        return id.length;
    }

    @Override
    public boolean isViewFromObject(View view, Object object){
        return view==object;
    }

    @Override
    public Object instantiateItem(ViewGroup container, int position){

        ImageView imageView = new ImageView(context);
        imageView.setBackgroundResource(id[position]);
        container.addView(imageView);

        if(position == id.length-1){
            imageView.setOnClickListener(new View.OnClickListener(){
                @Override
                public void onClick(View v){
                    Intent intent = new Intent(context, HomeActivity.class);
                    intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                    context.startActivity(intent);
                    ((Activity)context).finish();

                    SharedPreferences sp = context.getSharedPreferences("welcome", Context.MODE_PRIVATE);
                    SharedPreferences.Editor edit = sp.edit();
                    edit.putBoolean("isFirstRun",false).commit();
                }
            });
        }
        return imageView;
    }

    @Override
    public void destroyItem(ViewGroup container, int position, Object object){
        container.removeView((View)object);
    }

}
